package com.atguigu.eduService.service.impl;

import com.atguigu.commonUtils.Result;
import com.atguigu.eduService.client.VodClient;
import com.atguigu.eduService.entity.EduVideo;
import com.atguigu.eduService.mapper.EduVideoMapper;
import com.atguigu.eduService.service.EduVideoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;

/**
 * <p>
 * 课程视频 服务实现类
 * </p>
 *
 * @author Chen Wen bo
 * @since 2021-07-22
 */
@Service
@Transactional
public class EduVideoServiceImpl extends ServiceImpl<EduVideoMapper, EduVideo> implements EduVideoService {

    @Resource
    private EduVideoMapper eduVideoMapper;
    @Resource
    private VodClient vodClient;

    //添加小节
    @Override
    public void addVideo(EduVideo eduVideo) {
        eduVideoMapper.insert(eduVideo);
    }

    //删除小节
    @Override
    public void deleteVideo(String id) {
        EduVideo eduVideo = eduVideoMapper.selectById(id);
        if (!StringUtils.isEmpty(eduVideo.getVideoSourceId())) {
            //根据视频id，远程调用实现视频删除
            Result<String> result = vodClient.deleteVideo(eduVideo.getVideoSourceId());
            if (result.getCode() == 3000) {
                throw new RuntimeException("删除视频出错了，熔断器");
            }
        }
        eduVideoMapper.deleteById(id);
    }

    //根据id查询小节数据
    @Override
    public EduVideo getVideo(String id) {
        return eduVideoMapper.selectById(id);
    }

    //修改小节
    @Override
    public void updateVideo(EduVideo eduVideo) {
        eduVideoMapper.updateById(eduVideo);
    }
}
